using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Snake : MonoBehaviour
{
    public GameUI gameUI;
    AudioSource music;

    [HideInInspector] public Vector2 direction;
    public float speed;

    public Transform bodyPrefab;

    public GameObject fail_window;

    public List<Transform> bodies = new List<Transform>();


    void Start()//初始化
    {
        Read_pifu();

        Time.timeScale = speed;
        ResetStage();
        music = GetComponent<AudioSource>();
        music.volume = GameObject.Find("数据").GetComponent<GameSystem>().yinxiao;
    }

    void Update()
    {
        Player_Move();
    }


    void Read_pifu()//读取皮肤数据
    {
        GameSystem system = GameObject.Find("数据").GetComponent<GameSystem>();
        bodyPrefab = system.snake_body[system.current_player_num];
        this.gameObject.GetComponent<SpriteRenderer>().sprite = system.snake_head[system.current_player_num];
    }
    void Player_Move()//移动控制
    {
        if(!fail_window.activeSelf)
        {
            if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
            {
                gameUI.game_play = true;
            }

            if (Input.GetKeyDown(KeyCode.W) && direction != Vector2.down)
            {
                direction = Vector2.up;
            }
            if (Input.GetKeyDown(KeyCode.S) && direction != Vector2.up)
            {
                direction = Vector2.down;
            }
            if (Input.GetKeyDown(KeyCode.A) && direction != Vector2.right)
            {
                direction = Vector2.left;
            }
            if (Input.GetKeyDown(KeyCode.D) && direction != Vector2.left)
            {
                direction = Vector2.right;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                direction *= 2;
            }
            else if (Input.GetKeyUp(KeyCode.Space))
            {
                direction /= 2;
            }
        }
        
    }

    private void FixedUpdate()
    {
        for (int i = bodies.Count-1; i >0; i--)
        {
            bodies[i].position=bodies[i-1].position;
        }
        transform.Translate(direction);
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Food"))//吃到食物
        {
            bodies.Add(Instantiate(bodyPrefab,transform.position,Quaternion.identity));
            GameObject.Find("数据").GetComponent<GameSystem>().item_nums[collision.gameObject.GetComponent<Food>().num]++;
            gameUI.AddScore();
        }

        if (collision.gameObject.tag == "Obstacle")//碰到障碍物
        {
            music.Play();
            fail_window.SetActive(true);
            direction = Vector2.zero;
            gameUI.game_play = false;
        }
        if((collision.gameObject.tag == "boom"))//碰到炸弹
        {
            gameUI.score -= 2;
            collision.gameObject.GetComponent<Bomb>().RandomPosition();
        }

    }

    void ResetStage()//重置
    {
        transform.position = Vector2.zero;
        direction = Vector2.zero;

        for (int i = 1; i < bodies.Count; i++)
        {
            Destroy(bodies[i].gameObject);
        }
        bodies.Clear();
        bodies.Add(transform);

        gameUI.ResetScore();
    }

    
}
